package com.palm.hubo2o.util;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.palm.hubo2o.R;
import com.palm.palmutils.widget.EasyRecyclerView;
import com.palm.palmutils.widget.refreshlayout.RefreshLayout;


/**
 * Created by dest1 on 2015/10/26.
 */
public abstract class LoadHelper {
    private static final int PER = 20;
    private int mFrom;
    private int mTotal;
    private int mEndPage;
    private RefreshLayout mRefreshLayout;
    private EasyRecyclerView mRecyclerView;
    private Context mContext;


    public void bindRefreshLayout(Context context, RefreshLayout refreshLayout, EasyRecyclerView recyclerView) {
        mContext = context;
        mRefreshLayout = refreshLayout;
        mRecyclerView = recyclerView;
        mRefreshLayout.setOnRefreshListener(mOnRefreshListener);
        mRecyclerView.addOnScrollListener(mOnScrollListener);
        mRefreshLayout.setHeaderColorSchemeResources(R.color.colorAccent, R.color.red, R.color.blue);
        mRefreshLayout.setFooterColorSchemeResources(R.color.colorAccent, R.color.red, R.color.blue, R.color.green);
    }

    public void firstLoad() {
        mRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mRefreshLayout.setHeaderRefreshing(true);
            }
        });
        getPageData(0, PER);
    }

    public abstract void getPageData(int from, int size);

    public void succeedLoad(int total) {
        mTotal = total;
        mFrom = mFrom + PER >= mTotal ? mTotal : mFrom + PER;
    }

    private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (!mRefreshLayout.isRefreshing() && mRefreshLayout.isAlmostBottom() && mFrom < mTotal) {
                // Get next page
                mRefreshLayout.setFooterRefreshing(true);
                mOnRefreshListener.onFooterRefresh();
            }
        }
    };

    private RefreshLayout.OnRefreshListener mOnRefreshListener = new RefreshLayout.OnRefreshListener() {
        @Override
        public void onHeaderRefresh() {
            mFrom = 0;
            getPageData(mFrom, PER);
//            if (mStartPage > 0) {
//                mCurrentTaskId = mIdGenerator.nextId();
//                mCurrentTaskType = TYPE_PRE_PAGE_KEEP_POS;
//                mCurrentTaskPage = mStartPage - 1;
//                getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
//            } else {
//                doRefresh();
//            }
        }

        @Override
        public void onFooterRefresh() {
            if (mFrom >= mTotal) {
                Toast.makeText(mContext, "没有更多了哦", Toast.LENGTH_SHORT).show();
                mRefreshLayout.setFooterRefreshing(false);
            } else {
                getPageData(mFrom, PER);
            }

//            if (mEndPage < mPages) {
//                // Get next page
//                mCurrentTaskId = mIdGenerator.nextId();
//                mCurrentTaskType = TYPE_NEXT_PAGE_KEEP_POS;
//                mCurrentTaskPage = mEndPage;
//                getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
//            } else if (mEndPage == mPages) {
//                // Refresh last page
//                mCurrentTaskId = mIdGenerator.nextId();
//                mCurrentTaskType = TYPE_REFRESH_PAGE;
//                mCurrentTaskPage = mEndPage - 1;
//                getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
//            } else {
//                Log.e(TAG, "Try to footer refresh, but mEndPage = " + mEndPage + ", mPages = " + mPages);
//            }
        }
    };

}
